<?php

namespace catchAdmin\permissions\model;

use catchAdmin\mbqy\tables\forms\Field;
use catcher\Utils;

trait DataRangScopeTrait
{
    /**
     * 数据范围查询
     *
     * @param $roles
     * @return mixed
     * @author JaguarJack <njphper@gmail.com>
     * @date 2020/6/6
     */
    public function dataRange($roles = [])
    {

        if (Utils::isSuperAdmin()) {

            return $this;
        }
        $userIds = $this->getDepartmentUserIdsBy($roles);

        if (empty($userIds)) {
            return $this;
        }


        return $this->whereIn($this->aliasField('creator_id'), $userIds);
    }

    /**
     * 获取部门IDs
     *
     * @param $roles
     * @return array
     * @author JaguarJack <njphper@gmail.com>
     * @date 2020/6/6
     */
    public function getDepartmentUserIdsBy($roles)
    {
        $userIds = [];

        $isAll = false;

        $user = request()->user();
        $customDeptId = request()->header("custom_dept_id", "");
        if (!empty($customDeptId) && !empty($user["other_dept"])) {
            //判断部门id是否再user表中
            $deptList = (new \catchAdmin\permissions\model\Department())->getDeptByIds($user["other_dept"]);

            if (!empty($deptList)) {
                $deptIds = array_column($deptList, "id");
                $deptName = array_column($deptList, "department_name", "id");
                if (in_array($customDeptId, $deptIds, true)) {
                    $user->department_id = $customDeptId;
                    $user->department_name = $deptName[$customDeptId];

                }
            }

        }
        if (empty($roles)) {
            $roles = $user->getRoles();
        }

        foreach ($roles as $role) {

            switch ($role->data_range) {
                case Roles::ALL_DATA:
                    $isAll = true;
//                if(Utils::isSuperAdmin()){
//                    $isAll = true;
//                }else{
//                    $isAll = true;
//                    //$departmentIds = array_merge(array_column($role->getDepartments()->toArray(), 'id'));
//
//                    //$userIds = $this->getUserIdsByDepartmentId($departmentIds);
//
//                }
                    break;
                case Roles::SELF_CHOOSE:
                    $departmentIds = array_merge(array_column($role->getDepartments()->toArray(), 'id'));
                    $userIds = $this->getUserIdsByDepartmentId($departmentIds);

                    break;

                case Roles::DEPARTMENT_DOWN_DATA:
                    // 查一下下级部门
                    $departmentIds = Department::getChildrenDepartmentIds($user->department_id);
                    $userIds = $this->getUserIdsByDepartmentId($departmentIds);
                    break;
                case Roles::DEPARTMENT_DATA:
                    $userIds = $this->getUserIdsByDepartmentId([$user->department_id]);
                    break;
                default:
                    break;
            }

            // 如果有全部数据 直接跳出
            if ($isAll) {
                break;
            } else {
                $userIds[] = $user->id;
            }
        }

        return $userIds;
    }

               

    /**
     * 获取UserID
     *
     * @time 2020年07月04日
     * @param array $id
     * @param string $field
     * @return array
     */
    protected function getUserIdsByDepartmentId(array $id, string $field = 'id')
    {
        return Users::whereIn('department_id', $id)->column($field);
    }


}
